package org.aaron.demos;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver.OnScrollChangedListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class SpinnerActivity extends Activity {

	private TextView datedisplay;
	private Button pickDate;
	private TextView timedisplay;
	private Button timepickDate;
	private Spinner spinner;
	private AutoCompleteTextView autoCompleteTextView;
	private SeekBar seekBar;
	private RatingBar ratingBar;
	private final static int DATA_DIALOG_ID = 0;
	private final static int TIME_DIALOG_ID = 1;
	private int year;
	private int month;
	private int day;
	private int hourOfDay;
	private int minute;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spinnerlayout);
		initActivity();
	}

	private void initActivity() {
		// spinner初始化
		spinner = (Spinner) this.findViewById(R.id.spinner);
		spinner.setOnItemSelectedListener(new MyItemSelectedListener());
		spinner.setAdapter(spinnerAapter());

		// pickDate初始化
		datedisplay = (TextView) this.findViewById(R.id.dateDisplay);
		pickDate = (Button) this.findViewById(R.id.pickDate);
		pickDate.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SpinnerActivity.this.showDialog(DATA_DIALOG_ID);
			}
		});
		final Calendar calendar = Calendar.getInstance();
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH);
		day = calendar.get(Calendar.DAY_OF_MONTH);
		updateDate();

		// timepickDate初始化
		timedisplay = (TextView) this.findViewById(R.id.timeDisplay);
		timepickDate = (Button) this.findViewById(R.id.timepickDate);
		timepickDate.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SpinnerActivity.this.showDialog(TIME_DIALOG_ID);
			}
		});
		hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
		minute = calendar.get(Calendar.MINUTE);
		updateTime();

		// autoCompleteTextView 初始化
		autoCompleteTextView = (AutoCompleteTextView) this.findViewById(R.id.myAutoCompleteView);
		autoCompleteTextView.setAdapter(autoCompleteAdpter());

		// seekBar初始化
		seekBar = (SeekBar) this.findViewById(R.id.mySeekBar);
		seekBar.setMax(100);
		seekBar.setOnSeekBarChangeListener(new MySeekBarListener());

		// ratingBar初始化
		ratingBar = (RatingBar) this.findViewById(R.id.myRatingBar);
		ratingBar.setOnRatingBarChangeListener(new MyRatingBar());
	}

	private ArrayAdapter<CharSequence> spinnerAapter() {
		/**
		 * 静态加载spinner数据 ArrayAdapter<CharSequence> arrayAdapter =
		 * ArrayAdapter.createFromResource(this, R.array.planets_array,
		 * android.R.layout.simple_spinner_item);
		 * arrayAdapter.setDropDownViewResource
		 * (android.R.layout.simple_spinner_dropdown_item);
		 * spinner.setAdapter(arrayAdapter);
		 */
		/** 动态加载spinner数据 */
		List<CharSequence> datalist = new ArrayList<CharSequence>();
		datalist.add("test1");
		datalist.add("test2");
		return new ArrayAdapter<CharSequence>(SpinnerActivity.this, R.layout.spinneritemlayout, R.id.textviewid, datalist);
	}

	private ArrayAdapter<String> autoCompleteAdpter() {

		return new ArrayAdapter<String>(SpinnerActivity.this, R.layout.autoitemlayout, R.id.autotextviewid, COUNTRIES);
	}

	private void updateDate() {
		datedisplay.setText(new StringBuilder().append(year).append("-").append(month + 1).append("-").append(day));
	}

	private void updateTime() {
		timedisplay.setText(new StringBuilder().append(hourOfDay).append("-").append(minute));
	}

	class MyItemSelectedListener implements OnItemSelectedListener {
		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			Toast.makeText(arg0.getContext(), arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_LONG).show();
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {

		switch (id) {
		case DATA_DIALOG_ID:
			return new DatePickerDialog(SpinnerActivity.this, new MyDateSetListener(), year, month, day);
		case TIME_DIALOG_ID:
			return new TimePickerDialog(SpinnerActivity.this, new MyTimeSetListener(), hourOfDay, minute, true);
		}

		return null;
	}

	private class MyDateSetListener implements OnDateSetListener {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			SpinnerActivity.this.year = year;
			SpinnerActivity.this.month = monthOfYear;
			SpinnerActivity.this.day = dayOfMonth;
			updateDate();
		}

	}

	private class MyTimeSetListener implements OnTimeSetListener {
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			SpinnerActivity.this.hourOfDay = hourOfDay;
			SpinnerActivity.this.minute = minute;
			updateTime();

		}
	}

	private class MySeekBarListener implements SeekBar.OnSeekBarChangeListener {

		// 进度改变时调用
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			Log.d("MySeekBarListener onProgressChanged", progress + "");
		}

		// 开始滑动时调用
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			Log.d("MySeekBarListener onStartTrackingTouch", seekBar.getProgress() + "");
		}

		// 结束滑动时调用
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			Log.d("MySeekBarListener onStopTrackingTouch", seekBar.getProgress() + "");
		}

	}

	private class MyRatingBar implements RatingBar.OnRatingBarChangeListener {

		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
			Log.d("MyRatingBar onRatingChanged", rating + "");
		}

	}

	static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
			"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
			"Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
			"Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands",
			"Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica",
			"Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
			"Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece",
			"Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
			"Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos", "Latvia",
			"Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
			"Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal",
			"Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas", "Norway", "Oman",
			"Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russia",
			"Rwanda", "Sqo Tome and Principe", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino",
			"Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands",
			"South Korea", "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand",
			"The Bahamas", "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
			"Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam",
			"Wallis and Futuna", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe" };

}
